// ignore_for_file: prefer_const_literals_to_create_immutables, prefer_const_constructors

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:time_manager/components/left_item.dart';
import 'package:time_manager/global/controller/record_item_controller.dart';

import 'package:time_manager/utils/DataBaseRecord.dart';
import 'package:time_manager/utils/RecordModel.dart';
import 'package:time_manager/utils/timeDeal.dart';

class CurrentView extends StatelessWidget {
  CurrentView({Key? key}) : super(key: key);

  final controller = Get.find<RecordItemController>();

  List<RecoreModel> fetchCurrentDate() {
    var temp = TimeUtil().getLocalTime(
        DateTime.now().millisecondsSinceEpoch.toString(),
        format: 'hour');
    var currentTime = DateTime.parse(temp).millisecondsSinceEpoch.toString();
    List<RecoreModel> filterList = [];
    controller.list.forEach((it) {
      if (it.dealtime == currentTime) {
        filterList.add(it);
      }
    });
    return filterList;
    // list.assignAll(result);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('今日日程')),
      floatingActionButton: FloatingActionButton(
        child: Text('新建\n日程'),
        onPressed: () async {
          Get.toNamed('/add_record');
          // controller.addRecord();
        },
      ),
      body: Container(
        // padding: EdgeInsets.only(bottom: 20),
        color: Color(0xffeeeeee),
        child: RefreshIndicator(
            child: Obx(() {
              var list = fetchCurrentDate();
              return list.isEmpty
                  ? ListView(
                      children: [
                        Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            SizedBox(
                              height: 100,
                            ),
                            Image.asset(
                              'assets/images/order_empty.png',
                              width: Get.width - 150,
                            ),
                            Text('您今天没有日程哦'),
                            Text('可以好好休息一下(*^▽^*)')
                          ],
                        ),
                      ],
                    )
                  : ListView.builder(
                      padding: EdgeInsets.only(bottom: 30),
                      itemCount: list.length,
                      itemBuilder: (_, index) {
                        RecoreModel temp = list[index];
                        return ItemContainer(
                          item: temp,
                          index: index,
                          controller: controller,
                        );
                      });
            }),
            onRefresh: controller.onRefresh),
      ),
    );
  }
}

class ItemContainer extends StatelessWidget {
  final RecoreModel item;
  final int index;
  final RecordItemController controller;
  const ItemContainer(
      {Key? key,
      required this.item,
      required this.index,
      required this.controller})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
        margin: const EdgeInsets.only(top: 10, left: 10, right: 10),
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
            color: Colors.white,
            boxShadow: [
              BoxShadow(
                color: Colors.white,
                blurRadius: 10,
              )
            ],
            borderRadius: BorderRadius.circular(5.0)),
        child: Row(
          mainAxisSize: MainAxisSize.min,
          children: [
            LeftItem(item: item),
            LayoutBuilder(
                builder: (BuildContext context, BoxConstraints constraints) {
              return Container(
                // color: Colors.green,
                child: Column(
                  mainAxisSize: MainAxisSize.max,
                  // mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(
                      item.status == '1' ? '未完成' : "已完成",
                      style: TextStyle(
                          color:
                              item.status == '1' ? Colors.red : Colors.green),
                    ),
                    Offstage(
                      offstage: item.status == '2',
                      child: Container(
                        margin: EdgeInsets.only(top: 6),
                        height: 27,
                        child: ElevatedButton(
                          child: Text('已完成'),
                          style: ElevatedButton.styleFrom(
                              minimumSize: Size(60, 27),
                              textStyle: TextStyle(fontSize: 13),
                              padding: EdgeInsets.all(0),
                              shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(20))),
                          onPressed: () {
                            Get.defaultDialog(
                                title: '提示',
                                content: Text('您确定已经完成了吗？'),
                                onConfirm: () async {
                                  controller.updateRcord(index);
                                  Get.back();
                                },
                                onCancel: () => Get.back());
                          },
                        ),
                      ),
                    )
                  ],
                ),
              );
            })
          ],
        ));
  }
}
